package com.lt.contract.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.NMContractNew;
import com.lt.contract.bean.utils.Constant;
import com.lt.contract.bean.utils.ResponseResult;
import com.lt.contract.bean.utils.Utils;
import com.lt.contract.service.NMNewContractService;
import com.lt.report.util.VeDate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping(value = "/contractNew")
public class NMNewContractController {

    @Autowired
    NMNewContractService nmNewContractService;

    /**
     * 获取  续签合同明细
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/queryContractList")
    public JSONArray queryContractList(@RequestParam String params){
        System.out.println(params);
        List<NMContractNew> contractList = null;
        try {
            Map<String,Object> map = JSON.parseObject(params);
            contractList = nmNewContractService.queryNewContractList(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(contractList));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  续签合同总数
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/queryContractListCount")
    public Long queryContractListCount(@RequestParam String params){
        try {
            Map<String,Object> map = JSON.parseObject(params);
            Long count = nmNewContractService.getNewContractListCount(map);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 新增合同明细
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid NMContractNew renew, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                String s = Utils.hasErrors(bindingResult);
                return new ResponseResult(false, "插入失败", s);
            }
            String code = nmNewContractService.insertSelective(renew);
            if(Constant.getErrorMsg.containsKey(code)){
                return  new ResponseResult(false, Constant.getErrorMsg.get(code));
            }
            return new ResponseResult(true, "插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false, "插入失败", e.getStackTrace());
        }
    }

    /**
     * 更新合同明细
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid NMContractNew renew, BindingResult bindingResult){
        try {
            if (bindingResult.hasErrors()) {
                String s = Utils.hasErrors(bindingResult);
                return new ResponseResult(false, "update失败", s);
            }
            if (renew.getId() ==null || renew.getId().equals("")){
                return new ResponseResult(false, "update失败", "更新请传入主键");
            }
            String code = nmNewContractService.updateByPrimaryKeySelective(renew);
            if(Constant.getErrorMsg.containsKey(code)){
               return new ResponseResult(false, Constant.getErrorMsg.get(code));
            }
            return new ResponseResult(true,"更新成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false,"更新失败",e.getStackTrace());
        }
    }

    /**
     * 删除合同明细  update data_tag 0 有效数据 1 删除数据
     * @param params
     * @return
     */
    @CrossOrigin
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody String params){
        try {
            JSONObject renew = JSON.parseObject(params);
            if(renew.getString("id") == null ||renew.getString("id").equals("")){
                return new ResponseResult(false, "delete失败", "请传入主键");
            }else if(renew.getString("staffId") == null ||renew.getString("staffId").equals("")){
                return new ResponseResult(false, "delete失败", "");
            }
            NMContractNew record = JSON.parseObject(params,NMContractNew.class);
            nmNewContractService.deleteByPrimaryKey(record);
            return new ResponseResult(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false,"删除失败",e.getStackTrace());
        }
    }

    @PostMapping(value = "/upload")
    @ResponseBody
    @CrossOrigin
    public String uploadExcel(HttpServletRequest request) {
        log.info("\n【请求地址】：{}\n【请求时间】：{}\n", "/upload", VeDate.getNowDate());
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multipartRequest.getFile("file");
            String staffId = request.getHeader("staffId");
            if (file.isEmpty()) {
                return "文件不能为空";
            }
            InputStream inputStream = file.getInputStream();
            log.info("\n【请求地址】：{}\n【请求时间】：{}\n【上传文件名】：{}", "/upload", VeDate.getNowDate(), file.getOriginalFilename());
            String result=nmNewContractService.getBankListByExcel(inputStream, file.getOriginalFilename(),staffId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败。";
        }
    }

}
